package cn.yunyichina.provider.base.service.impl;

import cn.yunyichina.provider.base.dao.OrgUserOpenidDao;
import cn.yunyichina.provider.base.entity.OrgUserOpenid;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.iface.entity.common.ResponseObject;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.dubbo.common.utils.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Joe.zhou on 2017/8/23.
 */
@Service
public class OrgopenidServiceImpl {

    @Autowired
    private OrgUserOpenidDao orgUserOpenidDao;

    private static final Logger logger = LoggerFactory.getLogger(OrgopenidServiceImpl.class);

    /**
     * 根据条件获取系统用户OpenId信息
     * @param json
     * @interfaceName: yyt.base.orgopenid.get
     * @return
     */
    public ResponseObject getOrgOpenid(String json){
        logger.info("OrgopenidServiceImpl.getOrgOpenid json: "+ JsonUtils.toJsonString(json));
        ResponseObject responseObject = new ResponseObject();
        try {
            logger.info("根据条件获取系统用户OpenId信息 json -->" + JsonUtils.toJsonString(json));
            OrgUserOpenid openid = JsonUtils.parseObject(json,OrgUserOpenid.class);
            //根据按使用时间倒叙排序
            List<String> orderColumns = new ArrayList<String>();
            orderColumns.add("UPDATE_TIME");
            List<OrgUserOpenid> orgUserList = orgUserOpenidDao.select(openid,orderColumns,false);
            if (CollectionUtils.isEmpty(orgUserList)){
                responseObject.setResultCodeAndMessage(Response.FAILURE_CODE,"查询openId信息不存在");
                return  responseObject;
            }
            responseObject.setResultCodeAndMessageAndResult(Response.SUCCESS_CODE,Response.SUCCESS_MESSAGE,orgUserList);
        }catch (Exception e){
            logger.error("查询openId信息出错",e);
            responseObject.setResultCodeAndMessage(Response.FAILURE_CODE,"查询openId信息出错");
        }
        return  responseObject;
    }


    /**
     * 修改系统用户OpenId信息
     * @param json
     * @interfaceName:  yyt.base.orgopenid.update
     * @return
     */
    public ResponseObject updateOrgOpenid(String json) {
        logger.info("OrgopenidServiceImpl.updateOrgOpenid json: "+JsonUtils.toJsonString(json));
        ResponseObject response = new ResponseObject();
        OrgUserOpenid openid = JsonUtils.parseObject(json, OrgUserOpenid.class);

        try {
            orgUserOpenidDao.update(openid);
            response.setResultCodeAndMessage(Response.SUCCESS_CODE , Response.FAILURE_MESSAGE);
        } catch (Exception e) {
            logger.error("[OrgopenidServiceImpl.updateOrgOpenid] 更新OpenId信息失败" ,e);
            response.setResultCodeAndMessage(Response.FAILURE_CODE , "更新OpenId信息失败");
        }

        return response;
    }



    /**
     * 新增系统用户OpenId信息
     * @param json
     * @interfaceName: yyt.base.orgopenid.add
     * @return
     */
    public ResponseObject addOrgOpenid(String json) {
        logger.info("OrgopenidServiceImpl.addOrgOpenid json: "+JsonUtils.toJsonString(json));
        ResponseObject response = new ResponseObject();
        OrgUserOpenid openid = JsonUtils.parseObject(json, OrgUserOpenid.class);

        try {
            orgUserOpenidDao.insert(openid);
            response.setResultCodeAndMessage(Response.SUCCESS_CODE , Response.FAILURE_MESSAGE);
        } catch (Exception e) {
            logger.error("[OrgopenidServiceImpl.addOrgOpenid] 新增OpenId信息失败" ,e);
            response.setResultCodeAndMessage(Response.FAILURE_CODE , "新增OpenId信息失败");
        }

        return response;
    }
}
